package com.koron.css2.newProjectCenter.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjWeekdayConfiguration;
import com.koron.css2.newProjectCenter.bean.PjWeekdayMonth;
import com.koron.css2.newProjectCenter.mapper.PjWeekdayConfigurationMapper;
import com.koron.css2.newProjectCenter.mapper.PjWeekdayMonthMapper;
import com.koron.css2.newProjectCenter.vo.PjProcessNodeTimePostponeListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ；lwc
 * @Date: 2023/11/06/17:41
 * @Description:报装工作日配置添加接口
 */
public class PjWeekdayConfigurationAdd implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjWeekdayConfiguration bean = JsonUtils.objectToPojo(req.getData(), PjWeekdayConfiguration.class);
            if (ObjectUtil.isNull(bean)||StringUtils.isBlank(bean.getWeekdayName())||ObjectUtil.isEmpty(bean.getMonths())
            ||ObjectUtil.isNull(bean.getMorningBeginDate())||ObjectUtil.isNull(bean.getMorningEndDate())||ObjectUtil.isNull(bean.getAfternoonBeginDate())||ObjectUtil.isNull(bean.getAfternoonEndDate())){
                throw new RuntimeException("缺少参数");
            }


            PjWeekdayConfigurationMapper mapper = factory.getMapper(PjWeekdayConfigurationMapper.class);
            PjWeekdayMonthMapper monthMapper = factory.getMapper(PjWeekdayMonthMapper.class);
            if (StringUtils.isBlank(bean.getId())){
                bean.setId(IdUtil.simpleUUID());
                bean.setCreateTime(LocalDateTime.now().toString());
                bean.setCreateAccount(userInfo.getUserInfo().getAcount());
                bean.setCreateName(userInfo.getUserInfo().getName());
            }else {
                mapper.deleteById(bean.getId());
                monthMapper.deleteByConfigurationId(bean.getId());
                bean.setUpdateAccount(userInfo.getUserInfo().getAcount());
                bean.setUpdateName(userInfo.getUserInfo().getName());
                bean.setCreateTime(LocalDateTime.now().toString());
            }
            mapper.insertAll(bean);
            List<PjWeekdayMonth> monthList=new ArrayList<>();

            for (Integer month : bean.getMonths()) {
                int count=monthMapper.countByMonth(month);
                if (count>0){
                    throw new RuntimeException(month+"月份已使用请重新选择");
                }
                PjWeekdayMonth pjWeekdayMonth = new PjWeekdayMonth();
                BeanUtil.copyProperties(bean,pjWeekdayMonth);
                pjWeekdayMonth.setId(IdUtil.simpleUUID());
                pjWeekdayMonth.setMonth(month);
                pjWeekdayMonth.setConfigurationId(bean.getId());
                pjWeekdayMonth.setUpdateTime(LocalDateTime.now().toString());
                monthList.add(pjWeekdayMonth);
            }
            monthMapper.batchInsert(monthList);


            MessageBean<PjProcessNodeTimePostponeListVO> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "添加成功", PjProcessNodeTimePostponeListVO.class);
            return info;
        }catch(Exception e){
            logger.error("报装工作日配置添加失败"+e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "报装工作日配置添加失败:"+e.getMessage(), null);
        }
    }

}
